package android_testsuite.mytest.sqlite;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.io.File;

/**
 * Created by renhui on 2015/4/22.
 */
public class DatabaseAdapter extends SQLiteOpenHelper {
    /**
     * 数据库参数
     */
    private static final String mDbFolder = "my_frame_sqlite";  //SQLite db folder
    private static final String mDbName = "myframe.db";         //SQLite db name
    private static final int mDbVersion = 1;                    //SQLite db Version

    private SQLiteDatabase mDatabase;

    // 构造函数
    public DatabaseAdapter(Context context) {
        super(context, mDbName, null, mDbVersion);

        // 获取SQLite数据库文件路径
        String pathToSQLite = context.getDatabasePath(mDbFolder).getPath();
        Log.d(DatabaseAdapter.class.getSimpleName(), "SQLite Dir:" + pathToSQLite);
        // 创建数据库文件
        File sqliteDir = new File(pathToSQLite);

        // 检测 文件是否存在且是否路径是否是路径
        if (!sqliteDir.exists() || !sqliteDir.isDirectory()) {
            sqliteDir.mkdirs();
        }

        // 创建数据库文件
        new File(sqliteDir + File.separator + mDbName);
        Log.d(DatabaseAdapter.class.getSimpleName(), "DB Path:" + pathToSQLite + File.separator + mDbName);
        mDatabase = SQLiteDatabase.openDatabase(pathToSQLite + File.separator + mDbName, null, SQLiteDatabase.CREATE_IF_NECESSARY);

        // 获取当前数据库的版本信息
        int version = mDatabase.getVersion();
        Log.d(DatabaseAdapter.class.getSimpleName(), "Current Version:" + mDbVersion + ", DB Version:" + version);

        // 开始Database事务，针对数据库版本
        mDatabase.beginTransaction();

        try {
            if (version == 0) {
                onCreate(mDatabase);
            } else {
                onUpgrade(mDatabase, version, mDbVersion);
            }
            mDatabase.setVersion(mDbVersion);
            mDatabase.setTransactionSuccessful();
        } catch (SQLException e) {
            Log.d(DatabaseAdapter.class.getSimpleName(), e.toString());
        } finally {
            mDatabase.endTransaction();
        }
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        // 版本号增加1，相应的调用SqliteV的onCreate方法
        new SqliteV1().onCreate(sqLiteDatabase);
    }

    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        // 版本号增加1，相应的调用SqliteV的onUpgrade方法
        Log.d(DatabaseAdapter.class.getSimpleName(), "oldVersion:" + oldVersion + ", new :" + newVersion);
        // 更新数据库结构
        switch (oldVersion) {
            case 1:
                // if SqliteV2 exist
                // new SqliteV2().onUpgrade(mDatabase);
            case 2:
                // if SqliteV3 exist
                // new SqliteV3().onUpgrade(mDatabase);
            default:
                break;
        }
    }

    public SQLiteDatabase getDatabase() {
        return mDatabase;
    }
}
